package cn.gzjp.modules.wx.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.gzjp.common.utils.MyBeanUtils;
import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.web.BaseController;
import cn.gzjp.common.utils.StringUtils;
import cn.gzjp.modules.wx.entity.WxCorpAgent;
import cn.gzjp.modules.wx.service.WxCorpAgentService;

/**
 * 企业号应用Controller
 * @author zengcp
 * @version 2017-09-18
 */
@Controller
@RequestMapping(value = "${adminPath}/wx/wxCorpAgent")
public class WxCorpAgentController extends BaseController {

	@Autowired
	private WxCorpAgentService wxCorpAgentService;
	
	
	/**
	 * 企业号应用列表页面
	 */
	@RequiresPermissions("wx:wxCorpAgent:list")
	@RequestMapping(value = {"list", ""})
	public String list(WxCorpAgent wxCorpAgent, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WxCorpAgent> page = wxCorpAgentService.findPage(new Page<WxCorpAgent>(request, response), wxCorpAgent); 
		model.addAttribute("page", page);
		return "modules/wx/wxCorpAgentList";
	}

	/**
	 * 查看，增加，编辑企业号应用表单页面
	 */
	@RequiresPermissions(value={"wx:wxCorpAgent:view","wx:wxCorpAgent:add","wx:wxCorpAgent:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(WxCorpAgent wxCorpAgent, Model model) {
		model.addAttribute("wxCorpAgent", wxCorpAgent);
		return "modules/wx/wxCorpAgentForm";
	}

	/**
	 * 保存企业号应用
	 */
	@RequiresPermissions(value={"wx:wxCorpAgent:add","wx:wxCorpAgent:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(WxCorpAgent wxCorpAgent, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, wxCorpAgent)){
			return form(wxCorpAgent, model);
		}
		if(!wxCorpAgent.getIsNewRecord()){//编辑表单保存
			WxCorpAgent t = wxCorpAgentService.get(wxCorpAgent.getId());//从数据库取出记录的值
			if(t==null){
				wxCorpAgentService.save(wxCorpAgent,wxCorpAgent.getId());//保存
				addMessage(redirectAttributes, "保存企业号应用成功");
				return redirect("/wx/wxCorpAgent/?repage");
			}
			MyBeanUtils.copyBeanNotNull2Bean(wxCorpAgent, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			wxCorpAgentService.save(t);//保存
		}else{//新增表单保存
			
		}
		addMessage(redirectAttributes, "保存企业号应用成功");
		return redirect("/wx/wxCorpAgent/?repage");
	}
	
	/**
	 * 删除企业号应用
	 */
	@RequiresPermissions("wx:wxCorpAgent:del")
	@RequestMapping(value = "delete")
	public String delete(WxCorpAgent wxCorpAgent, RedirectAttributes redirectAttributes) {
		wxCorpAgentService.delete(wxCorpAgent);
		addMessage(redirectAttributes, "删除企业号应用成功");
		return redirect("/wx/wxCorpAgent/?repage");
	}
	
	/**
	 * 批量删除企业号应用
	 */
	@RequiresPermissions("wx:wxCorpAgent:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			wxCorpAgentService.delete(wxCorpAgentService.get(id));
		}
		addMessage(redirectAttributes, "删除企业号应用成功");
		return redirect("/wx/wxCorpAgent/?repage");
	}
	

	@ModelAttribute
	public WxCorpAgent get(@RequestParam(required=false) String id) {
		WxCorpAgent entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = wxCorpAgentService.get(id);
		}
		if (entity == null){
			entity = new WxCorpAgent();
		}
		return entity;
	}
	

}